﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer.Web.WebControls;

namespace EPiServer.Municipality.Web.UI.WebControls {
    /// <summary>
    /// Summary description for FileSystemList.
    /// </summary>
    /// <remarks>
    /// </remarks>
    [ToolboxData("<{0}:FileSystemList runat=server></{0}:FileSystemList>")]
    public class FileSystemList : CompositeDataBoundControl {
        /* *******************************************************************
         *  Properties 
         * *******************************************************************/
        #region public ITemplate HeaderTemplate
        /// <summary>
        /// Get/Sets the ItemTemplate of the IndexList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof (FileSystemListData))]
        public ITemplate HeaderTemplate { get; set; }
        #endregion
        #region public ITemplate FileTemplate
        /// <summary>
        /// Get/Sets the ItemTemplate of the IndexList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof (FileSystemListData))]
        public ITemplate FileTemplate { get; set; }
        #endregion
        #region public ITemplate FooterTemplate
        /// <summary>
        /// Get/Sets the ItemTemplate of the IndexList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof (FileSystemListData))]
        public ITemplate FooterTemplate { get; set; }
        #endregion
        /* *******************************************************************
         *  Methods 
         * *******************************************************************/
        #region protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataSource"></param>
        /// <param name="dataBinding"></param>
        /// <returns></returns>
        protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding) {
            var count = 0;
            if (dataBinding) {
                InstantiateTemplate(HeaderTemplate);
                foreach (object fileSystemListItem in dataSource) {
                    var item = fileSystemListItem as FileSystemItem;
                    if (item != null) {
                        var data = new FileSystemListData(item, count++);
                        if (item.IsFile) {
                            InstantiateTemplate(FileTemplate, data);
                        }
                    }
                }
                InstantiateTemplate(FooterTemplate);
            }
            return count;
        }
        #endregion
        #region private void InstantiateTemplate(ITemplate template, Control container)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="template"></param>
        /// <param name="container"></param>
        private void InstantiateTemplate(ITemplate template, Control container) {
            if (template != null) {
                template.InstantiateIn(container);
                Controls.Add(container);
                container.DataBind();
            }
        }
        #endregion
        #region private void InstantiateTemplate(ITemplate template)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="template"></param>
        private void InstantiateTemplate(ITemplate template) {
            InstantiateTemplate(template, new Control());
        }
        #endregion
        #region protected override void Render(HtmlTextWriter writer)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer) {
            base.RenderContents(writer);
        }
        #endregion
        /* *******************************************************************
         *  Event methods 
         * *******************************************************************/
    }
}